package com.zy.openchat.core.model.file;

import android.os.Parcel;
import android.os.Parcelable;

import com.ja.openchat.R;
import com.zy.openchat.Constants;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class DocumentInfo implements Parcelable {
    //文件原始名称 - 不用于显示
    public String file_name;
    //文件类型
    //public String mime_type;
    //文件
    public FileInfo document;

    public void updateFile(FileInfo file) {
        if (document != null&&file!=null&&document.id == file.id) {
            document = file;
        }
    }

    public String getRemoteFileId(){
        if(document!=null&&document.remote!=null){
            return document.remote.id;
        }
        return "";
    }


    public long totalSize() {
        if (document != null)
            return document.expected_size;
        return 0;
    }

    public long donwloadSize() {
        if (document != null && document.local != null)
            return document.local.downloaded_size;
        return 0;
    }

    /**
     * 文件本地路径
     *
     * @return 本地路径
     */
    public String localFilePath() {
        if (document != null && document.local != null && document.local.isExist()) {
            return document.local.path;
        }
        //本地路径，根据file_name获取
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userFilePath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return path;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean isFileDownloadCompleted(){
        return document!=null&&document.isCompleted();
    }

    /**
     * 文件是否已下载
     *
     * @return 是否已下载
     */
    public boolean isFileDownloaded() {
        if (document != null && document.local != null && document.local.isExist()) {
            return true;
        }
        //本地路径，根据file_name判断
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userFilePath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    private static String getFileExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return null;
    }

    /**
     * 是否图片
     *
     * @param fileName 文件名称
     * @return
     */
    public static boolean isImageFile(String fileName) {
        if (!StringUtils.isEmpty(fileName)) {
            String suffix = getFileExtensionName(fileName);
            if (!StringUtils.isEmpty(suffix)) {
                return "jpg".equalsIgnoreCase(suffix) || "jpeg".equalsIgnoreCase(suffix) || "png".equalsIgnoreCase(suffix);
            }
        }
        return false;
    }

    public static boolean isGifeFile(String fileName) {
        if (!StringUtils.isEmpty(fileName)) {
            String suffix = getFileExtensionName(fileName);
            if (!StringUtils.isEmpty(suffix)) {
                return "gif".equalsIgnoreCase(suffix) ;
            }
        }
        return false;
    }


    /**
     * 是否视频
     *
     * @param fileName 文件名称
     * @return
     */
    public static boolean isVideoFile(String fileName) {
        if (!StringUtils.isEmpty(fileName)) {
            String suffix = getFileExtensionName(fileName);
            if (!StringUtils.isEmpty(suffix)) {
                return "mp4".equalsIgnoreCase(suffix) || "avi".equalsIgnoreCase(suffix) || "mov".equalsIgnoreCase(suffix) ||
                        "wmv".equalsIgnoreCase(suffix) || "asf".equalsIgnoreCase(suffix) || "navi".equalsIgnoreCase(suffix) ||
                        "3gp".equalsIgnoreCase(suffix) || "mkv".equalsIgnoreCase(suffix) || "f4v".equalsIgnoreCase(suffix) ||
                        "rmvb".equalsIgnoreCase(suffix) || "webm".equalsIgnoreCase(suffix);
            }
        }
        return false;
    }

    /**
     * 文件对应的类型图标
     *
     * @param fileName 文件名称
     * @return
     */
    public static int fileTypeIcon(String fileName) {
        if (!StringUtils.isEmpty(fileName)) {
            String suffix = getFileExtensionName(fileName);
            if (!StringUtils.isEmpty(suffix)) {
                if ("txt".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_txt;
                } else if ("docx".equalsIgnoreCase(suffix) || "doc".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_word;
                } else if ("xls".equalsIgnoreCase(suffix) || "xlsx".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_excel;
                } else if ("ppt".equalsIgnoreCase(suffix) || "pptx".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_ppt;
                } else if ("pdf".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_pdf;
                } else if ("psd".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_psd;
                } else if (isImageFile(fileName)) {
                    return R.drawable.file_type_image;
                } else if (isVideoFile(fileName)) {
                    return R.drawable.file_type_video;
                } else if ("mp3".equalsIgnoreCase(suffix) || "wma".equalsIgnoreCase(suffix) || "wav".equalsIgnoreCase(suffix) ||
                        "mod".equalsIgnoreCase(suffix) || "ra".equalsIgnoreCase(suffix) || "cd".equalsIgnoreCase(suffix) ||
                        "md".equalsIgnoreCase(suffix) || "asf".equalsIgnoreCase(suffix) || "aac".equalsIgnoreCase(suffix) ||
                        "vqf".equalsIgnoreCase(suffix) || "mid".equalsIgnoreCase(suffix) || "ogg".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_audio;
                } else if ("rar".equalsIgnoreCase(suffix) || "zip".equalsIgnoreCase(suffix) || "7z".equalsIgnoreCase(suffix) ||
                        "gz".equalsIgnoreCase(suffix) || "tar".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_zip;
                } else if ("html".equalsIgnoreCase(suffix) || "htm".equalsIgnoreCase(suffix) || "shtml".equalsIgnoreCase(suffix) ||
                        "shtm".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_web;
                } else if ("exe".equalsIgnoreCase(suffix)) {
                    return R.drawable.file_type_exe;
                } else {
                    return R.drawable.file_type_default;
                }
            }
        }
        return R.drawable.file_type_default;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.file_name);
        dest.writeParcelable(this.document, flags);
    }

    public void readFromParcel(Parcel source) {
        this.file_name = source.readString();
        this.document = source.readParcelable(FileInfo.class.getClassLoader());
    }

    public DocumentInfo() {
    }

    protected DocumentInfo(Parcel in) {
        this.file_name = in.readString();
        this.document = in.readParcelable(FileInfo.class.getClassLoader());
    }

    public static final Creator<DocumentInfo> CREATOR = new Creator<DocumentInfo>() {
        @Override
        public DocumentInfo createFromParcel(Parcel source) {
            return new DocumentInfo(source);
        }

        @Override
        public DocumentInfo[] newArray(int size) {
            return new DocumentInfo[size];
        }
    };
}
